package com.educoart.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.route53.AmazonRoute53Client;
import com.amazonaws.services.route53.model.Change;
import com.amazonaws.services.route53.model.ChangeAction;
import com.amazonaws.services.route53.model.ChangeBatch;
import com.amazonaws.services.route53.model.ChangeResourceRecordSetsRequest;
import com.amazonaws.services.route53.model.HostedZone;
import com.amazonaws.services.route53.model.RRType;
import com.amazonaws.services.route53.model.ResourceRecord;
import com.amazonaws.services.route53.model.ResourceRecordSet;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringPool;

public class DNSUtil {
	
	public static void createSubDomain(long companyId, String subdomain) {
		
		String awsAccessKey = StringPool.BLANK;
		try {
			awsAccessKey = PrefsPropsUtil.getString(companyId, "aws-access-key");
		} catch (SystemException e) {
			e.printStackTrace();
		}
		String awsSecretKey = StringPool.BLANK;
		try {
			awsSecretKey = PrefsPropsUtil.getString(companyId, "aws-secret-key");
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
        AWSCredentials credentials = new BasicAWSCredentials(
        		awsAccessKey, awsSecretKey);
        
        AmazonRoute53Client client = new AmazonRoute53Client(credentials);
        
        ChangeResourceRecordSetsRequest changeResourceRecordSetsRequest =
                new ChangeResourceRecordSetsRequest();
        
        String hostedZoneId = StringPool.BLANK;
        List<HostedZone> hostedZones = client.listHostedZones().getHostedZones();
        
        for (HostedZone hostedZone: hostedZones) {
        	if (hostedZone.getName().startsWith(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID))) {
        		hostedZoneId = hostedZone.getId();
        	}
        }
        
        changeResourceRecordSetsRequest.setHostedZoneId(hostedZoneId);
        
        ChangeBatch changeBatch = new ChangeBatch();
        
        List<Change> changes = new ArrayList<Change>();
        
        ResourceRecordSet resourceRecordSet = new ResourceRecordSet();
        
        resourceRecordSet.setType(RRType.A);
        resourceRecordSet.setName(subdomain + IConstants.ORG_SITE_URL);
        resourceRecordSet.setTTL(60l);
        
        String ipAddress = getServerIP();
		
		ResourceRecord resourceRecord = new ResourceRecord();
        resourceRecord.setValue(ipAddress);
        List<ResourceRecord> resourceRecords = new ArrayList<ResourceRecord>();
        resourceRecords.add(resourceRecord);
        
        resourceRecordSet.setResourceRecords(resourceRecords);
        
        changes.add(new Change(ChangeAction.CREATE, resourceRecordSet));
        
        changeBatch.setChanges(changes);
        
        changeResourceRecordSetsRequest.setChangeBatch(changeBatch);
        
        client.changeResourceRecordSets(changeResourceRecordSetsRequest);
    }
	
	public static String getServerIP() {
		
		String ipAddress = null;
        URL checkipURL = null;
		try {
			checkipURL = new URL("http://checkip.amazonaws.com");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
        BufferedReader in = null;
        try {
            try {
				in = new BufferedReader(new InputStreamReader(checkipURL.openStream()));
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				ipAddress = in.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return ipAddress;
    }		
}